﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.MailClient.Win.Forms;
using DevExpress.MailClient.Win.Forms.Campagne;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraGrid.Controls;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraGrid.Columns;
using Zero.DataLayer;
using Zero.DataLayer.Repository;
using Zero.POCO.Compagne;
using Zero.Presenter;

namespace DevExpress.MailClient.Win.Modules {

    public partial class Campagne : BaseModule, ICampagneView{


        public List<Compaign> campagnes { get; set; }
        public CampagnePresenter compaignPresenter;



        //Compaign currentMessage;
        RibbonControl ribbon;
        FindControlManager findControlManager = null;
        //Timer messageReadTimer;
        public override string ModuleName { get { return Properties.Resources.MailName; } }
        public Campagne() {
            InitializeComponent();
            
        }

        private void Campagne_Load(object sender, EventArgs e) {
            compaignPresenter = new CampagnePresenter(this, new CampagneRepository());
            compaignPresenter.Display();
            gridControlCampagne.DataSource = campagnes;
            //CalcPreviewIndent();
        }
       
        internal override void InitModule(Utils.Menu.IDXMenuManager manager, object data) {
            base.InitModule(manager, data);
            ribbon = manager as RibbonControl;
            ShowAboutRow();
        }
        void ShowAboutRow() {
            Timer tmr = new Timer();
            tmr.Interval = 100;
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Start();
        }
        void tmr_Tick(object sender, EventArgs e) {
            //lockUpdateCurrentMessage = false;
            FocusRow(0);
            ((Timer)sender).Stop();
        }
        void FocusRow(int rowHandle) {
        }
      
        internal override void HideModule() {
            //lockUpdateCurrentMessage = true;
            //focusedRowHandle = gridView1.FocusedRowHandle;
        }
        protected override void LookAndFeelStyleChanged() {
            base.LookAndFeelStyleChanged();
            ColorHelper.UpdateColor(ilColumns, gridControlCampagne.LookAndFeel);ColorHelper.UpdateColor(ilColumns, gridControlDetail.LookAndFeel);
        }

        void EditMessage(int rowHandle) {
            if(rowHandle < 0) return;
          //  Message message = gridView1.GetRow(rowHandle) as Message;
            //if(message != null)
              //  EditMessage(message, false, gcFrom.Caption);
        }


        
        protected internal override void ButtonClick(string tag) {
            switch(tag) {
              
                case TagResources.NewCampaign:
                    CreateNewCampagne();
                    break;
             
            }
        }


     

        void CreateNewCampagne() {
            var campagne = new Compaign();
            //message.MailType = MailType.Draft;
            EditMessage(campagne, true, null);

        }
        void EditMessage(Compaign campagne, bool newMessage, string caption) {
            Cursor.Current = Cursors.WaitCursor;
           var form = new frmEditCompagne(campagne, newMessage, caption);
            //form.Load += OnEditMailFormLoad;
            //form.FormClosed += OnEditMailFormClosed;
            form.Location = new Point(OwnerForm.Left + (OwnerForm.Width - form.Width) / 2, OwnerForm.Top + (OwnerForm.Height - form.Height) / 2);
            form.ShowDialog();

            if (form.DialogResult == DialogResult.Yes)
            {
                compaignPresenter.WriteChanges(form.sourceCompaign);
                compaignPresenter.Display();
                gridControlCampagne.DataSource = campagnes;
            }

            Cursor.Current = Cursors.Default;
        }
        void CreateReplyAllMailMessages() {
            /*foreach (int row in gridView1.GetSelectedRows())
                CreateReplyMailMessage(row);*/
        }

       
   
     
      

        string CreateReplyMessageText(string text, string to, DateTime originalMessageDate) {
            using (RichEditDocumentServer server = new RichEditDocumentServer()) {
                server.HtmlText = text;
                QuoteReplyMessage(server, to, originalMessageDate);
                return server.HtmlText;
            }
        }
        string CreateForwardMessageText(string text, string to) {
            using (RichEditDocumentServer server = new RichEditDocumentServer()) {
                server.HtmlText = text;
                QuoteForwardMessage(server, to);
                return server.HtmlText;
            }
        }
        void QuoteReplyMessage(RichEditDocumentServer server, string to, DateTime originalMessageDate) {
            QuoteMessage(server);
            Document document = server.Document;
            string replyHeader = String.Format(
                Properties.Resources.ReplyText,
                to, originalMessageDate
            );
            document.InsertText(document.Range.Start, replyHeader);
        }
        void QuoteMessage(RichEditDocumentServer server) {
            Document document = server.Document;
            ParagraphCollection paragraphs = document.Paragraphs;
            foreach (Paragraph paragraph in paragraphs) {
                DocumentRange range = paragraph.Range;
                if (document.GetTableCell(range.Start) == null && !paragraph.IsInList) {
                    document.InsertText(range.Start, ">> ");
                }
            }
        }
        void QuoteForwardMessage(RichEditDocumentServer server, string to) {
            Document document = server.Document;
            string replyHeader = Properties.Resources.ForwardTextStart;
            document.InsertText(document.Range.Start, replyHeader);
            document.AppendText(Properties.Resources.ForwardTextStart);
        }

        protected internal override void MessagesDataChanged(DataSourceChangedEventArgs args) {
            partName = args.Caption;
         //   gridControl1.DataSource = args.List;
            if(args.Type == MailType.Deleted) {
              /*  gcDate.Caption = Properties.Resources.DateDeleted;
                gcFrom.Caption = Properties.Resources.FromDeleted;*/
                OwnerForm.FilterColumnManager.UpdateColumnsCaption(Properties.Resources.DateDeleted, Properties.Resources.FromDeleted);
            } else if(args.Type == MailType.Inbox) {
            /*    gcDate.Caption = Properties.Resources.DateInbox;
                gcFrom.Caption = Properties.Resources.FromInbox;*/
                OwnerForm.FilterColumnManager.UpdateColumnsCaption(Properties.Resources.DateInbox, Properties.Resources.FromInbox);
            } else {
              /*  gcDate.Caption = Properties.Resources.DateOutbox;
                gcFrom.Caption = Properties.Resources.FromOutbox;*/
                OwnerForm.FilterColumnManager.UpdateColumnsCaption(Properties.Resources.DateOutbox, Properties.Resources.FromOutbox);
            }
            if(FindControl != null) {
                FindControl.FindEdit.Properties.NullValuePrompt = StringResources.GetSearchPrompt(args.Type);
                FindControl.FindEdit.Properties.NullValuePromptShowForEmptyValue = true;
                if(findControlManager == null)
                    findControlManager = new FindControlManager(ribbon, FindControl);
            }
           // UpdateCurrentMessage();
        }
        FindControl FindControl {
            get {
            /*    foreach(Control ctrl in gridControl1.Controls) {
                    FindControl ret = ctrl as FindControl;
                    if(ret != null) return ret;
                }*/
                return null;
            }
        }
       
       
        void MakeFocusedRowVisible() {
           // gridView1.MakeRowVisible(gridView1.FocusedRowHandle);
        }
        protected internal override void SendKeyDown(KeyEventArgs e) {
            base.SendKeyDown(e);
            if(e.KeyData == (Keys.E | Keys.Control)) {
                if(FindControl != null) {
                    FindControl.FindEdit.Focus();
                }
            }
        }
      
        protected override bool AllowZoomControl { get { return true; } }
        

    /*    private void gridView1_ColumnPositionChanged(object sender, EventArgs e) {
            CalcPreviewIndent();
        }

        void CalcPreviewIndent() {
            int indent = 0;
          
        }*/


    }
}
